<!DOCTYPE html>
<html lang="en">
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <title>智能组卷系统</title>
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">
</head>
<body class="hold-transition">
<div id="app">
    <div class="app-container">
        <div class="box">
            <!-- 筛选器 -->
            <div class="filter-container">
                <el-input placeholder="题干" v-model="pagination.selection" style="width: 200px;"
                          class="filter-item"></el-input>
                <el-button @click="handleSelect()" class="dalfBut">查询</el-button>
                <el-button @click="resetSelection()" class="dalfBut">重置</el-button>
                <el-button type="warning" class="butT" @click="handleFind0()">高级筛选</el-button>
                <el-button type="primary" class="butT" @click="handleCreate()">新增试题</el-button>
            </div>

            <!-- 数据表格 -->
            <el-table size="small" current-row-key="id" row-height="60px" :data="dataList" border>
                <el-table-column type="index" align="center" label="序号"></el-table-column>
                <el-table-column label="题干" align="center">
                    <template slot-scope="scope">
                        <div style="white-space: nowrap; text-overflow: ellipsis; overflow: hidden;">
                            {{scope.row.question }}
                        </div>
                    </template>
                </el-table-column>
                <el-table-column prop="type" label="题目类型" width="150px" align="center"></el-table-column>
                <el-table-column prop="difficulty" label="难度系数" width="150px" align="center"></el-table-column>

                <el-table-column label="章节" width="150px" align="center">
                    <template slot-scope="scope">
                        {{ scope.row.chapter}}-{{ scope.row.section}}
                    </template>
                </el-table-column>
                <el-table-column label="操作" width="250px" align="center">
                    <template slot-scope="scope">
                        <el-button v-if="userInfo.status===2" type="primary" size="mini"
                                   @click="handleUpdate(scope.row)">查看/编辑
                        </el-button>
                        <el-button v-if="userInfo.status===1" type="primary" size="mini"
                                   @click="handleUpdate(scope.row)">查看
                        </el-button>
                        <el-button v-if="userInfo.status===2" type="danger" size="mini"
                                   @click="handleDelete(scope.row)">删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>

            <!-- 分页组件 -->
            <div class="pagination-container">
                <el-pagination
                        class="pagination"
                        @current-change="handleCurrentChange"
                        :current-page="pagination.currentPage"
                        :page-size="pagination.pageSize"
                        layout="total,prev,pager,next,jumper"
                        :total="pagination.total">
                </el-pagination>
            </div>

            <!-- 新增弹窗 -->
            <div class="add-form">
                <el-dialog title="添加题目" :visible.sync="addFormVisible">
                    <el-form ref="dataAddForm" :model="formData" :rules="rules" label-position="right">
                        <el-row>
                            <el-col :span="7">
                                <el-form-item label="题型" prop="type" label-width="100px">
                                    <el-select v-model="formData.type" clearable>
                                        <el-option label="选择题" value="选择题"></el-option>
                                        <el-option label="填空题" value="填空题"></el-option>
                                        <el-option label="简答题" value="简答题"></el-option>
                                        <el-option label="判断题" value="判断题"></el-option>
                                        <el-option label="计算题" value="计算题"></el-option>
                                        <el-option label="其它题" value="其它"></el-option>
                                    </el-select>

                                </el-form-item>
                            </el-col>

                            <el-col :span="7">
                                <el-form-item label="难度" prop="difficulty" label-width="100px">
                                    <el-input v-model="formData.difficulty"/>
                                </el-form-item>
                            </el-col>

                            <el-col :span="6">
                                <el-form-item label="章节" prop="chapter" label-width="100px">
                                    <el-input v-model="formData.chapter" placeholder="章"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="4">
                                <el-form-item label="--" prop="section" label-width="35px">
                                    <el-input v-model="formData.section" placeholder="节"/>
                                </el-form-item>
                            </el-col>
                        </el-row>

                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="题干" label-width="100px">
                                    <el-input v-model="formData.question" type="textarea" :rows="4"
                                              placeholder="请输入题干"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="答案" label-width="100px">
                                    <el-input v-model="formData.answer" type="textarea" :rows="4"
                                              placeholder="请输入答案"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>

                    <div slot="footer" class="dialog-footer">
                        <el-button @click="addFormVisible = false">取消</el-button>
                        <el-button type="primary" @click="handleAdd()">确定</el-button>
                    </div>
                </el-dialog>
            </div>

            <!-- 编辑弹窗 -->
            <div class="add-form">
                <el-dialog title="试题内容" :visible.sync="editFormVisible" :close-on-click-modal="false">
                    <el-form ref="dataAddForm" :model="formData" :rules="rules" label-position="right"
                             :disabled="isFormDisabled()">
                        <el-row>
                            <el-col :span="7">
                                <el-form-item label="题型" prop="type" label-width="100px">
                                    <el-select v-model="formData.type" clearable>
                                        <el-option label="选择题" value="选择题"></el-option>
                                        <el-option label="填空题" value="填空题"></el-option>
                                        <el-option label="简答题" value="简答题"></el-option>
                                        <el-option label="判断题" value="判断题"></el-option>
                                        <el-option label="计算题" value="计算题"></el-option>
                                        <el-option label="其它题" value="其它"></el-option>
                                    </el-select>

                                </el-form-item>
                            </el-col>

                            <el-col :span="7">
                                <el-form-item label="难度" prop="difficulty" label-width="100px">
                                    <el-input v-model="formData.difficulty"/>
                                </el-form-item>
                            </el-col>

                            <el-col :span="6">
                                <el-form-item label="章节" prop="chapter" label-width="100px">
                                    <el-input v-model="formData.chapter" placeholder="章"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="4">
                                <el-form-item label="--" prop="section" label-width="35px">
                                    <el-input v-model="formData.section" placeholder="节"/>
                                </el-form-item>
                            </el-col>
                        </el-row>

                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="题干" label-width="100px">
                                    <el-input v-model="formData.question" type="textarea" :rows="4"
                                              placeholder="请输入题干"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="答案" label-width="100px">
                                    <el-input v-model="formData.answer" type="textarea" :rows="4"
                                              placeholder="请输入答案"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>

                    <div slot="footer" class="dialog-footer" v-if="userInfo.status===2">
                        <el-button @click="editFormVisible = false">取消</el-button>
                        <el-button type="primary" @click="handleEdit()">确定</el-button>
                    </div>
                </el-dialog>
            </div>

            <!-- 高级搜索弹窗 -->
            <div class="add-form">
                <el-dialog title="高级搜索" :visible.sync="selectFormVisible" width="660px">
                    <el-form ref="dataSelectForm" :model="selectData" label-position="right">
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="题型" prop="type" label-width="100px">
                                    <el-select v-model="selectData.type" clearable>
                                        <el-option label="选择题" value="选择题"></el-option>
                                        <el-option label="填空题" value="填空题"></el-option>
                                        <el-option label="简答题" value="简答题"></el-option>
                                        <el-option label="判断题" value="判断题"></el-option>
                                        <el-option label="计算题" value="计算题"></el-option>
                                        <el-option label="其它题" value="其它"></el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>

                            <el-col :span="7">
                                <el-form-item label="难度" prop="difficultyMin" label-width="85px">
                                    <el-input v-model="selectData.difficultyMin" placeholder="最低难度"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="5">
                                <el-form-item label="~" prop="difficultyMax" label-width="35px">
                                    <el-input v-model="selectData.difficultyMax" placeholder="最高难度"/>
                                </el-form-item>
                            </el-col>
                        </el-row>

                        <el-row>
                            <el-col :span="7">
                                <el-form-item label="章节" prop="chapter" label-width="100px">
                                    <el-input v-model="selectData.chapter" placeholder="章"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="5">
                                <el-form-item label="--" prop="section" label-width="35px">
                                    <el-input v-model="selectData.section" placeholder="节"/>
                                </el-form-item>
                            </el-col>
                        </el-row>

                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="题干" label-width="100px">
                                    <el-input v-model="selectData.question" type="textarea" :rows="3"
                                              placeholder="请输入题干"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>

                    <div slot="footer" class="dialog-footer">
                        <el-button @click="selectFormVisible = false">取消</el-button>
                        <el-button type="primary" @click="handleFind()">搜索</el-button>
                    </div>
                </el-dialog>
            </div>
        </div>
    </div>
</div>
</body>
<script src="../js/vue.js"></script>
<script src="../plugins/elementui/index.js"></script>
<script type="text/javascript" src="../js/jquery.min.js"></script>
<script src="../js/axios.min.js"></script>
<script>
    var vue = new Vue({

        el: '#app',
        data: {
            page: 1,//1为普通模式,2为高级搜索模式
            pagination: {
                currentPage: 1,  //当前页码
                pageSize: 10,    //每页记录
                total: 0,        //总记录
                selection: "",    //搜索框
            },
            dataList: [],//当前页要展示的列表数据
            formData: {
                id:"",
                status:"",
                year:"",
                question:"",
                answer:"",
                chapter:"",
                section:"",
                difficulty:"",
                type:"",
            },//表单数据
            formData2: {
                id:"",
                status:"",
                year:"",
                question:"",
                answer:"",
                chapter:"",
                section:"",
                difficulty:"",
                type:"",
            },//编辑前表单数据
            selectData: {
                question: "",
                type: "",
                difficultyMin: "",
                difficultyMax: "",
                answer: "",
                chapter: "",
                section: ""
            },//高级搜索表单数据
            addFormVisible: false,//控制表单是否可见
            editFormVisible: false,//编辑表单是否可见
            selectFormVisible: false,//高级搜索表单是否可见
            rules: {//校验规则
                answer: [{required: true, message: '答案为必填项', trigger: 'blur'}],
                question: [{required: true, message: '题干为必填项', trigger: 'blur'}]
            }
        },

        //钩子函数，VUE对象初始化完成后自动执行
        created() {
            const userInfo = window.localStorage.getItem('userInfo')
            if (userInfo) {
                this.userInfo = JSON.parse(userInfo)
            }
            this.getAll();
        },

        methods: {
            getAll() {
                if (this.page === 1) {
                    param = "?question=" + this.pagination.selection;
                } else if (this.page === 2) {
                    param = "?question=" + this.selectData.question;
                    param += "&type=" + this.selectData.type;
                    param += "&chapter=" + this.selectData.chapter;
                    param += "&section=" + this.selectData.section;
                    param += "&difficultyMin=" + this.selectData.difficultyMin;
                    param += "&difficultyMax=" + this.selectData.difficultyMax;
                }
                console.log(param);
                axios.get("/questions/" + this.pagination.currentPage + "/" + this.pagination.pageSize + param).then((res) => {
                    this.pagination.pageSize = res.data.data.size;
                    this.pagination.currentPage = res.data.data.current;
                    this.pagination.total = res.data.data.total;
                    this.dataList = res.data.data.records;
                });
            },

            //搜索
            handleSelect() {
                this.page = 1;
                this.getAll();
            },

            //重置搜索框
            resetSelection() {
                this.page = 1;
                this.pagination.selection = "";
                this.getAll();
            },

            //分页
            handleCurrentChange(currentPage) {
                this.pagination.currentPage = currentPage;
                this.getAll()
            },

            //弹出高级搜索窗口
            handleFind0() {
                this.selectFormVisible = true;
                this.resetForm();
            },

            //高级搜索
            handleFind() {
                this.page = 2;
                this.selectFormVisible = false;
                this.getAll();
            },

            //弹出添加窗口
            handleCreate() {
                this.addFormVisible = true;
                this.resetForm();
            },

            //重置表单
            resetForm() {
                this.formData = {};
            },

            //添加
            handleAdd() {
                axios.post("/questions", this.formData).then((res) => {
                    if (res.data.code === 20010) {
                        this.addFormVisible = false;
                        this.$message.success("添加成功");
                    } else if (res.data.code === 20011) {
                        this.$message.error(res.data.msg);
                    } else {
                        this.$message.error(res.data.msg);
                    }
                }).finally(() => {
                    this.getAll();
                });
            },

            //弹出编辑窗口
            handleUpdate(row) {
                axios.get("/questions/" + row.id).then((res) => {
                    if (res.data.code === 20040) {
                        this.formData = res.data.data;
                        this.formData2.chapter= this.formData.chapter;
                        this.formData2.section= this.formData.section;
                        this.formData2.difficulty= this.formData.difficulty;
                        this.formData2.question= this.formData.question;
                        this.formData2.answer= this.formData.answer;
                        this.formData2.type= this.formData.type;
                        this.formData2.year= this.formData.year;
                        this.formData2.status= this.formData.status;
                        this.formData2.id= this.formData.id;
                        this.editFormVisible = true;
                    } else {
                        this.$message.error(res.data.msg);
                    }
                });
            },

            //编辑
            handleEdit() {
                let postData = {
                    formData: this.formData,
                    formData2: this.formData2
                }
                axios.put("/questions", postData).then((res) => {
                    //如果操作成功，关闭弹层，显示数据
                    if (res.data.code === 20030) {
                        this.editFormVisible = false;
                        this.$message.success("修改成功");
                    } else {
                        this.$message.error("修改失败");
                    }
                }).finally(() => {
                    this.getAll();
                });
            },

            //表单是否可编辑
            isFormDisabled() {
                if (this.userInfo.status === 1)
                    return true; // 这里返回true表示表单被禁用
                return false

            },

            // 删除
            handleDelete(row) {
                //1.弹出提示框
                this.$confirm("此操作永久删除当前数据，是否继续？", "提示", {
                    type: 'info'
                }).then(() => {
                    //2.做删除业务
                    axios.put("/questions/delete", row).then((res) => {
                        if (res.data.code === 20020) {
                            this.$message.success("删除成功");
                        } else {
                            this.$message.error("删除失败");
                        }
                    }).finally(() => {
                        this.getAll();
                    });
                }).catch(() => {
                    //3.取消删除
                    this.$message.info("取消删除操作");
                });
            },

        }
    })
</script>
</html>